package com.bckj.freshnow.order.trade.ons.producer;

import cn.hutool.core.date.DateUtil;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.bckj.fastboot.core.jackson.JsonUtils;
import com.bckj.fastboot.mq.ons.AbstractOnsProducer;
import com.bckj.freshnow.common.constant.TradeOrderMQConstants;
import com.bckj.freshnow.order.model.TradeOrderMsg;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RequiredArgsConstructor
public class TradeOrderOnsProducer extends AbstractOnsProducer<TradeOrderMsg> {

    private Producer producer;

    @Override
    protected Message buildMessage(TradeOrderMsg msg) {
        Message message = new Message(TradeOrderMQConstants.TOPIC, msg.getTag(), JsonUtils.toJsonStr(msg).getBytes());
        if (msg.getDelaySeconds() != null) {
            message.setStartDeliverTime(DateUtil.offsetSecond(new Date(), msg.getDelaySeconds()).getTime());
        }
        return message;
    }

    @Override
    protected Producer getProducer() {
        return producer;
    }
}
